package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.dtos.SensitivePageDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import com.heima.wemedia.utils.SensitiveWordUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author rong
 */
@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    private final WmSensitiveMapper wmSensitiveMapper;
    private final SensitiveWordUtil sensitiveWordUtil;
    @Override
    public ResponseResult queryPageSensitive(SensitivePageDto dto) {
        dto.jiancha();
        IPage<WmSensitive> p = wmSensitiveMapper.selectPage(new Page<>(dto.getPage(), dto.getSize()), new LambdaQueryWrapper<WmSensitive>()
                .like(dto.getName() != null || dto.getName() != "", WmSensitive::getSensitives, dto.getName())
                .orderByDesc(WmSensitive::getCreatedTime));
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int)p.getTotal());
        result.setData(p.getRecords());
        return result;
    }

    @Override
    public ResponseResult addSenistive(SensitiveDto dto) {
        if(StringUtils.isEmpty(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getCreatedTime()==null){
            dto.setCreatedTime(new Date());
        }
        WmSensitive sensitive = wmSensitiveMapper.selectOne(new LambdaQueryWrapper<WmSensitive>()
                .eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(sensitive!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        WmSensitive wmSensitive=new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        wmSensitive.setCreatedTime(dto.getCreatedTime());
        wmSensitive.setId(dto.getId());
        wmSensitiveMapper.insert(wmSensitive);
        SensitiveWordUtil.initMap(wmSensitiveMapper.selectList(null)
                .stream()
                .map(wmSensitiveO -> wmSensitiveO.getSensitives())
                .collect(Collectors.toList()));
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult modifySenistive(SensitiveDto dto) {
        if(StringUtils.isEmpty(dto.getSensitives())||dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getCreatedTime()==null){
            dto.setCreatedTime(new Date());
        }
        WmSensitive sensitive = wmSensitiveMapper.selectById(dto.getId());
        if(sensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmSensitive wmSensitive=new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        wmSensitive.setCreatedTime(dto.getCreatedTime());
        wmSensitive.setId(dto.getId());
        wmSensitiveMapper.updateById(wmSensitive);
        SensitiveWordUtil.initMap(wmSensitiveMapper.selectList(null)
                .stream()
                .map(wmSensitiveO -> wmSensitiveO.getSensitives())
                .collect(Collectors.toList()));
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult deleteSenistive(String id) {
        if (StringUtils.isEmpty(id)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitiveMapper.deleteById(id);
        SensitiveWordUtil.initMap(wmSensitiveMapper.selectList(null)
                .stream()
                .map(wmSensitiveO -> wmSensitiveO.getSensitives())
                .collect(Collectors.toList()));
        return ResponseResult.okResult(null);
    }

    @Override
    public Boolean panduanSenistive(String content) {
        if(content.length()>150||StringUtils.isEmpty(content)){
            return false;
        }
        Map<String, Integer> stringIntegerMap = sensitiveWordUtil.matchWords(content);
        if (!stringIntegerMap.values().stream().allMatch(value->value==0)) {
            log.info("文本有敏感词，需人工校验");
            return false;
        }
        return true;
    }
}
